package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.qms.service.AttractionService;
import cn.buk.tms.dto.AttractionInfoDto;
import cn.buk.tms.dto.AttractionPriceDto;
import cn.buk.tms.dto.response.TicketOrderResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 景点
 * @author yfdai
 */
@RestController
public class AttractionsController extends BaseController {

    @Autowired
    private AttractionService service;

    /**
     * 保存景点信息
     */
    @PostMapping(value = "/attraction")
    public JsonResult saveAttractionInfo(HttpServletRequest request,
                                         @ModelAttribute AttractionInfoDto dto
    ) {
        dto.setEnterpriseId(getEnterpriseId(request));
        return service.saveAttractionInfo(getUserId(request), dto);
    }

    @GetMapping("/attractions")
    public CommonDto<AttractionInfoDto> searchAttractions(HttpServletRequest request,
                                                          @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                                          @RequestParam(value = "pageSize", defaultValue = "20") final int pageSzie
    ) {
        CommonSearchCriteria sc = new CommonSearchCriteria();
        sc.setPageNo(pageNo);
        sc.setPageSize(pageSzie);

        List<AttractionInfoDto> list = service.searchAttractions(getUserId(request), sc);

        CommonDto<AttractionInfoDto> dto = new CommonDto<>();
        dto.setDataList(list);
        dto.setPage(sc.getPage());

        return dto;
    }


    @GetMapping("/attraction/{id}")
    public AttractionInfoDto searchAttractionById(HttpServletRequest request,
                                                  @PathVariable("id") int id) {
        final int enterpriseId = getEnterpriseId(request);

        return service.getAttractionInfoById(enterpriseId, id);
    }

    @PostMapping("/attraction/{id}/price")
    public JsonResult saveAttractionPrice(HttpServletRequest request,
                                          @PathVariable("id") final int attractionId,
                                          @RequestParam("startDate") final Date startDate,
                                          @RequestParam("endDate") final Date endDate,
                                          @RequestParam("price") final int price,
                                          @RequestParam("remark") final String remark
    ) {
        return service.saveAttractionPrice(getUserId(request), attractionId, startDate, endDate, price, remark);
    }

    @GetMapping("/attraction/{id}/prices")
    public CommonDto<AttractionPriceDto> searchAttractionPrice(HttpServletRequest request,
                                                          @PathVariable("id") final int attractionId,
                                                          @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                                          @RequestParam(value = "pageSize", defaultValue = "20") final int pageSize
    ) {
        CommonSearchCriteria sc = new CommonSearchCriteria();
        sc.setPageNo(pageNo);
        sc.setPageSize(pageSize);

        List<AttractionPriceDto> datas = service.searchAttractionPrice(getUserId(request), attractionId, sc);

        CommonDto<AttractionPriceDto> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    @DeleteMapping(value = "/attraction/{id}/price/{priceId}")
    public JsonResult deleteAttractionPrice(HttpServletRequest request,
                                            @PathVariable("id") final int attractionId,
                                            @PathVariable("priceId") final int priceId
    ) {
        return service.deleteAttractionPrice(getUserId(request), attractionId, priceId);
    }

    /**
     * 查找门票订单
     */
    @GetMapping("/attraction/ticket/orders")
    public CommonDto<TicketOrderResponseDto> searchTicketOrders(HttpServletRequest request,
                                                                @ModelAttribute("sc") SearchFlightOrderCriteria sc
    ) {
        List<TicketOrderResponseDto> datas = service.searchTicketOrders(getUserId(request), sc);

        CommonDto<TicketOrderResponseDto> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 查找门票订单
     */
    @GetMapping("/attraction/ticket/order/{id}")
    public TicketOrderResponseDto getTicketOrder(HttpServletRequest request,
                                                 @PathVariable("id") int orderId
    ) {
        return service.getTicketOrder(getUserId(request), orderId);
    }

    /**
     * 取消订单
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @PostMapping(value = "/attraction/ticket/order/{orderId}/status/canceled")
    public JsonResult cancelTicketOrder(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId,
                                        @RequestParam(value = "remark", required = false) String remark) {
        return service.cancelTicketOrder(getUserId(request), orderId, remark);
    }

    /**
     * 我来处理订单
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @PostMapping(value = "/attraction/ticket/order/{orderId}/status/processing")
    public JsonResult processTicketOrder(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId,
                                        @RequestParam(value = "remark", required = false) String remark) {
        return service.processTicketOrder(getUserId(request), orderId, remark);
    }

    /**
     * 门票订单处理完毕
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @PostMapping(value = "/attraction/ticket/order/{orderId}/status/finished")
    public JsonResult finishTicketOrder(HttpServletRequest request,
                                       @PathVariable("orderId") final int orderId,
                                       @RequestParam("remark") String remark) {
        return service.finishTicketOrder(getUserId(request), orderId, remark);
    }

    @RequestMapping(value = "/attraction/ticket/order/{orderId}/history")
    public List<OperationLog> searchTicketOrderHistory(HttpServletRequest request,
                                                      @PathVariable("orderId") final int orderId) {
        return service.searchTicketOrderHistory(getUserId(request), orderId);
    }
}
